package vsqlbuilder.gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.JTextField;

import vsqlbuilder.gui.diagram.DiagramModel;
import vsqlbuilder.logic.LogicModel;
import vsqlbuilder.logic.postgresql.connection;

public class MainController {

	public LogicModel Logic;
	public DiagramModel Model;
	private TablesTree tablestree;

	// Toolbar constants
	public static final int OPENCONN = 0;
	public static final int CLOSECONN = 1;
	public static final int JOIN = 2;
	public static final int ARROW = 3;


	public MainController() {

	}

	public void registerModel(Dimension modelSize) {
		this.Model = new DiagramModel(modelSize.width, modelSize.height);
		this.Logic = new LogicModel();
	}

	// TODO: [BUG #1] This is a temporary fix for a metrics problem for fonts,
	// should be fixed if possible.
	// Problem: We need first to create the graph font to determine the metrics,
	// how this can be done without that.
	public void registerMetrics() {
		this.Model.FONT_METRICS = this.Model.view.getGraphics().getFontMetrics(
				this.Model.FONT2);
	}

	public void registerTree(TablesTree tablestree) {
		this.tablestree = tablestree;
	}

	// Repaint the query model to avoid sync errors of image with logic model.
	public void repaintModel() {
		this.Model.view.repaint();
	}

	public void registerConnection(JTextField[] textFields) {
		if (this.Logic.DatabaseConnection != null) {
			if (this.Logic.DatabaseConnection.isOpenConnection()) {
				this.Logic.DatabaseConnection.closeConnection();
			}
			this.Logic.DatabaseConnection.setHostName(textFields[0].getText());
			this.Logic.DatabaseConnection.setPort(Integer
					.parseInt(textFields[1].getText()));
			this.Logic.DatabaseConnection.setDatabaseName(textFields[2]
					.getText());
			this.Logic.DatabaseConnection.setUser(textFields[3].getText());
			this.Logic.DatabaseConnection.setPassword(textFields[4].getText());
		} else {
			this.Logic.DatabaseConnection = new connection(textFields[0]
					.getText(), textFields[2].getText(), Integer
					.parseInt(textFields[1].getText()),
					textFields[3].getText(), textFields[4].getText());
		}
		if (this.Logic.DatabaseConnection.openConnection()) {
			this.Logic.reverseingDB();
			tablestree.constructTree();
		}
	}

	public void exitApplication() {
		if (Logic.DatabaseConnection != null)
			Logic.DatabaseConnection.closeConnection();
		System.exit(0);
	}

	public void emptyTree() {
		this.tablestree.emptyTree();
		this.Logic.emptyDatabaseModel();
		this.tablestree.lastX=10;
		this.tablestree.lastY=10;
		this.tablestree.maxY=0;
	}

	public void emptyDiagram(){
		this.Model.clearModel();
	}
	
	void registerToolBarGroup(ToolBarButtonGroup group) {

		group.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent event) {

				AbstractButton button = (AbstractButton) event.getSource();
				MainController.this.Model.view.menuOption = button.getMnemonic();

				// TODO [low]: put a message in the status panel
				// statusPanel.setLeftMessage(button.getToolTipText());
			}
		});
	}

	// Register

}
